home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: judgemi@ix.netcom.com(Michael Judge )
- Newsgroups: comp.lang.c++
- Subject: Re: problem writing class to a file..... - temp.txt [1/1]
- Date: 5 Feb 1996 23:19:34 GMT
- Organization: Netcom
- Message-ID: <4f63a6$kqu@cloner2.ix.netcom.com>
- References: <4f5a5e$3ca@geraldo.cc.utexas.edu>
- NNTP-Posting-Host: bos-ma9-23.ix.netcom.com
- X-NETCOM-Date: Mon Feb 05 3:19:34 PM PST 1996
-
- In <4f5a5e$3ca@geraldo.cc.utexas.edu> wharris@mail.utexas.edu (W.
- Harris) writes:
- >
- >
- > The problem I'm having is in the code "outfile.write(char
- *)&.....".
- > The book I used to learn C, C++ indicates this should work. The
- > compiler error I'm getting is "sizeof cannot be used on a
- > function". Code has been reduced; the struct definition and the
- > constructor have been left out. I've also opened the file for
- binary
- > output. (not indicated in code)
- >
- >
- >
- > class CarData
- > {
- > private :
- >
- > char VIN[7],Stock[7],Tag[7],Tech[4],Color[12],Model[16],Year[5];
- > char EstAmt[6],Name[35],Address[32],City[19],State[3],Zip[6];
- > char Area[4],Prefix[4],Num[5],WArea[4],WPrefix[4],WNum[5];
- > char FnlAmt[6];
- >
- > public :
- >
- > CarData(struct astruct1 CarInf);
- > };
- >
- //*********************************************************************
- >
- >
- >void main()
- > {
- >
- >
- > CarData CarConst1(struct astruct1 CarInfo); //1st instance of
- cnstctr
- > outfile.write((char *)&CarConst1, sizeof(CarConst1));
- //*****ERROR IS OCCURRING HERE*
- > getch();
- > closegraph();
- > }
- >
- >
- //*******************************************************************
- >
- >
-
- One way to get around this is by using sizeof(CarData) not
- an object variable. Without the complex constructor the code you have
- will work fine.
-
- this is also O.K. if you code like this.
-
- void main()
- {
- astruct1 CarInfo;
- CarData CarConst1(CarInfo); //1st instance of cnstctr
- outfile.write((char *)&CarConst1, sizeof(CarConst1)); IS OCCURRING
- getch();
- closegraph();
- }
-
-
- if you declare the variable outside of the constructor, it works fine.
- Why?? I have no idea! are you using Visual C++ v1.52? I am and got the
- same result. if this is correct then I would have failed the test.
-
- MJ
-